package li.teng.android.xplanet;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.view.WindowManager.BadTokenException;



public class DialogManager {
	public static final int DIALOG_MISS_TTS = 1;
	public static final int DIALOG_MISS_GPS = 2;
	private AlertDialog m_AlertDialog;

	private PositionWiki activity;

	public DialogManager(PositionWiki activity) {
		this.activity = activity;
	}

	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setTitle("Title");
		builder.setPositiveButton(activity.getString(R.string.ok), null);
		switch (id) {
		case DIALOG_MISS_TTS:
			builder.setMessage(R.string.missing_tts);
			break;
		case DIALOG_MISS_GPS:
			builder.setMessage(R.string.missing_gps);
			break;
		} 
		m_AlertDialog = builder.create();
		return m_AlertDialog;
	}
	
	 protected void onPrepareDialog(int id, Dialog dialog) {
		    switch (id) {
		      case DIALOG_MISS_TTS:
		    	  	;
		        break;
		      case DIALOG_MISS_GPS:
		    	  	;
		        break;
		    }
	 }
	 
	  public void showMessageDialog(final int message, final boolean success) {
		    activity.runOnUiThread(new Runnable() {
		      public void run() {
		        AlertDialog dialog = null;
		        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		        builder.setMessage(activity.getString(message));
		        builder.setNegativeButton(activity.getString(R.string.ok), null);
		        builder.setIcon(success ? android.R.drawable.ic_dialog_info :
		          android.R.drawable.ic_dialog_alert);
		        builder.setTitle(success ? R.string.success : R.string.error);
		        dialog = builder.create();
		        dialog.show();
		      }
		    });
		  }
	  
	  public void showDialogSafely(final int id) {
		    activity.runOnUiThread(new Runnable() {
		      public void run() {
		        try {
		          activity.showDialog(id);
		        } catch (BadTokenException e) {
//		          Log.w(MyTracksConstants.TAG,
//		              "Could not display dialog with id " + id, e);
		        } catch (IllegalStateException e) {
//		          Log.w(MyTracksConstants.TAG,
//		              "Could not display dialog with id " + id, e);
		        }
		      }
		    });
		  }
}
